﻿using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace WpfApplication5
{
    [TemplatePart(Name = "PART_Cancel", Type = typeof(Button))]
    [TemplatePart(Name = "PART_Textbox", Type = typeof(TextBox))]
    public class SearchBox : Control
    {
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            var cancelButton = GetTemplateChild("PART_Cancel") as Button;
            cancelButton.Click += (s1, e1) => Text = string.Empty;
        }

        static SearchBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(SearchBox), new FrameworkPropertyMetadata(typeof(SearchBox)));
        }

        public SearchBox()
        {
            this.PreviewGotKeyboardFocus += (s1, e1) => ShowFocusAdorner();// AdornerLayer.GetAdornerLayer(this).Add(focusAdorner);
            this.PreviewLostKeyboardFocus += (s1, e1) => RemoveFocusAdorner();// AdornerLayer.GetAdornerLayer(this).Remove(focusAdorner);
        }

        private void ShowFocusAdorner()
        {
            var layer = AdornerLayer.GetAdornerLayer(this);
            if (layer != null)
            {
                if (layer.GetAdorners(this) == null)
                {
                    layer.Add(new SearchBoxFocusAdorner(this));
                }
                else if (layer.GetAdorners(this).ToList().OfType<SearchBoxFocusAdorner>().Count() < 1)
                {
                    layer.Add(new SearchBoxFocusAdorner(this));
                }
            }
        }

        private void RemoveFocusAdorner()
        {
            var layer = AdornerLayer.GetAdornerLayer(this);
            if (layer != null)
            {
                if (layer.GetAdorners(this) != null)
                {
                    layer.GetAdorners(this).OfType<SearchBoxFocusAdorner>().ToList().ForEach(p => layer.Remove(p));
                }
            }
        }

        public string Watermark
        {
            get { return (string)GetValue(WatermarkProperty); }
            set { SetValue(WatermarkProperty, value); }
        }

        public static readonly DependencyProperty WatermarkProperty =
            DependencyProperty.Register("Watermark", typeof(string), typeof(SearchBox), new UIPropertyMetadata("Search"));

        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(SearchBox), new UIPropertyMetadata(null));
    }

    public class SearchBoxFocusAdorner : Adorner
    {
        public SearchBoxFocusAdorner(UIElement arg)
            : base(arg)
        {
            this.IsHitTestVisible = false;
        }

        protected override void OnRender(DrawingContext dc)
        {
            if (pen == null)
            {
                pen = new Pen(new SolidColorBrush(Color.FromRgb(0x66, 0x99, 0xcc)), 2);
                pen.Freeze();
            }

            dc.DrawRoundedRectangle(null, pen, new Rect(this.RenderSize), 12, 12);
        }

        private Pen pen;
    }
}
